package sqlToGUI.charts.helperUtilities;


import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Hashtable;
import java.util.Properties;

import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;

import sqlToGUI.HarpalykeGUI1;


public class ArrayPowerMapRenderer implements TableCellRenderer {

	/**The maximum value the map will expect */
	private int maxValue;
	/**The minimum value the map will expect */
	private int minValue;

	/**The number of different shades the map will display */
	private int diffShades;
	/**The number of sub-arrays this map will display */
	private int numSubArrays;

	/**The factor used for taking an input value and changing its magnitude
	 * to a different value, smaller or greater, that can then be rounded
	 * and located in the hashtable */
	private double inputToHashFactor;

	/**Mapping from a value to the specifc color shade */
	private Hashtable<Integer, Color> valueToColor;

	/**Array holding the calcultated input values from the database */
	private double input[];

	/**Property file holding information about the different shades */
	private Properties val_col_prop;

	/**The font used to display numerical values */
	private Font font1 = new Font("monospaced", Font.BOLD, 
			(int)(18*HarpalykeGUI1.resizer));
	
	/**The text editor for changing the cell colours and writing in
	 * the numerical values */
	private JTextField editor;
	
	public ArrayPowerMapRenderer(int numSubArrays){
		this.numSubArrays = numSubArrays;
		this.init();
	}

	public void init(){

		/**Load the property file */
		try {
			val_col_prop = new Properties();
			val_col_prop.load(new FileInputStream
					("properties/ArrayPowerMap.properties"));
		} catch (FileNotFoundException e) {
			System.out.println("ArrayPowerMapRenderer: " +
			"Failed to read property file");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("ArrayPowerMapRenderer: " +
			"Failed to read property file");
			System.exit(1);
		}

		/**Initialize the variables that will be used to decide what color
		 * to assign to an array based on a value given */
		maxValue = Integer.parseInt(val_col_prop.getProperty("top_val"));
		minValue = Integer.parseInt(val_col_prop.getProperty("min_val"));

		diffShades = Integer.parseInt(val_col_prop.
				getProperty("number_of_shades"));

		inputToHashFactor = diffShades/(maxValue-minValue + 0.0);

		/**Initialize the hashtable as specified in the property file */
		valueToColor = new Hashtable<Integer, Color>();
		try{
			for(int i = 0; i < diffShades; i++){
				valueToColor.put(i, 
						Color.decode(val_col_prop.
								getProperty("color_shade"+(diffShades-i))));
			}
			valueToColor.put(20, 
					Color.decode(val_col_prop.
							getProperty("color_shade0")));
			
		} catch(NullPointerException e) {
			System.out.println("ArrayPowerMapRenderer: " +
			"Failed to initialize hashtable");
			e.printStackTrace();
		}

		/**Initialize the array for holding the input */
		input = new double [numSubArrays];
		
		/**Initialize the cell editor */
		editor = new JTextField();
		editor.setFont(font1);
		editor.setForeground(Color.BLACK);
		editor.setHorizontalAlignment(SwingConstants.CENTER);
	}

	/**Take the next calculated input for the array map */
	public void takeInput(double [] input){
		this.input = input;
	}

	/**The method that colours each corresponding cell based on the input
	 * value corresponding to it */
	public Component getTableCellRendererComponent(JTable table, 
			Object value, boolean isSelected,
			boolean hasFocus, int row, int column) {

		if (value != null)
			editor.setText(value.toString());

		/**No idea how this works but it sure performs well :) */
		try {
			for(int i = 0; i < numSubArrays; i++) {

				/**Round the input value to a value existing in the hashtable */
				int hashTypeValue = (int)
					Math.round(((input[i] + Math.abs(minValue))*inputToHashFactor));

				Color valueColor = valueToColor.get(hashTypeValue);

				if(column ==  i){
					editor.setText("" + (int)(input[i]));
					editor.setBackground(valueColor);
				}

			}
		} catch(NullPointerException e) {
			System.out.println("ArrayPowerMapRenderer: " +
					"Failed to read hex-color: ");
		}
		return editor;
	}
}